package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult addChannel(WmChannel wmChannel) {
        // 插入数据
        boolean save = save(wmChannel);
        // 封装返回结果
        if(save){
            return ResponseResult.okResult(200,"添加频道成功");
        }else {
            return ResponseResult.errorResult(6666,"频道已存在或者数据有误, 添加失败");
        }
    }

    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        //1.检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();

        //2.分页条件查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //状态精确查询
        if(dto.getStatus() != null){
            lambdaQueryWrapper.eq(WmChannel::getStatus,dto.getStatus());
        }

        //关键字模糊查询
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }

        page = page(page,lambdaQueryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        boolean update = update(wmChannel, wrapper.eq(WmChannel::getName, wmChannel.getName()));
        return update ? ResponseResult.okResult(200,"修改频道成功") : ResponseResult.errorResult(6666,"修改失败");
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        boolean b = removeById(id);
        return b ? ResponseResult.okResult(200,"删除频道成功") : ResponseResult.errorResult(6666,"删除失败");
    }
}